﻿using NETCore.Encrypt;
using System;
using System.Security.Cryptography;
using System.Text;

namespace CommonDevelop
{
    /// <summary>
    /// 加密工具
    /// </summary>
    public static class EncryptHelper
    {
        private const string AES_KEY = "Cm8y5cPhmIhaliTRPv9JdfexlvUZgrGD";
        private const string AES_IV = "Cm8y5cPhmIhaliTR";

        /// <summary>
        /// AES 加密, 不携带加密向量
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string AESEncrypt(string srcString)
        {
            return EncryptProvider.AESEncrypt(srcString, AES_KEY);
        }

        /// <summary>
        /// AES 加密, 携带加密向量
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string AESEncrypt_IV(string srcString)
        {
            return EncryptProvider.AESEncrypt(srcString, AES_KEY, AES_IV);
        }

        /// <summary>
        /// AES 解密, 不携带加密向量
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string AESDecrypt(string encryptedStr)
        {
            return EncryptProvider.AESDecrypt(encryptedStr, AES_KEY);
        }

        /// <summary>
        /// AES 解密, 携带加密向量
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string AESDecrypt_IV(string encryptedStr)
        {
            return EncryptProvider.AESDecrypt(encryptedStr, AES_KEY, AES_IV);
        }

        /// <summary>
        /// Base64 加密
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string Base64Encrypt(string srcString)
        {
            return EncryptProvider.Base64Encrypt(srcString, Encoding.UTF8);
        }

        /// <summary>
        /// Base64 加密
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns></returns>
        public static string Base64Encrypt(string srcString, Encoding encoding)
        {
            return EncryptProvider.Base64Encrypt(srcString, encoding);
        }

        /// <summary>
        /// 取得输入字符串的MD5哈希值
        /// </summary>
        /// <param name="argInput">输入字符串</param>
        /// <returns>MD5哈希值</returns>
        public static string Md5Hash(string argInput)
        {
            using (MD5 md5Hasher = MD5.Create())
            {
                byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(argInput));
                var sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("X2"));
                }
                return sBuilder.ToString();
            }
        }

        /// <summary>
        /// 验证MD5哈希值
        /// </summary>
        /// <param name="argInput">输入字符串</param>
        /// <param name="argHash">哈希值</param>
        /// <returns>相同返回TRUE,不同返回FALSE</returns>
        public static bool VerifyMd5Hash(string argInput, string argHash)
        {
            return string.Equals(Md5Hash(argInput), argHash, StringComparison.Ordinal);
        }

    }
}
